package com.hd.trans.widgets;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.hd.trans.R;
import com.hd.trans.utils.ViewClickUtil;


/**
 * @author qianjujun
 * @email qianjujun@163.com
 * @createTime 2019/5/29 15:35
 * @describe
 */
public class MyDialog<T extends ViewDataBinding> implements View.OnClickListener {

    protected Context mContext;
    protected Dialog dialog;
    protected T mDataBinding;


    public MyDialog(Context context, View contentView, boolean cancelAble) {
        mDataBinding = DataBindingUtil.bind(contentView);
        this.mContext = context;
        dialog = createDialog(context);
        //dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        dialog.setCancelable(cancelAble);
        dialog.setCanceledOnTouchOutside(cancelAble);
        dialog.setContentView(contentView);
        dialog.setOnDismissListener(dialog -> MyDialog.this.onDismiss());


    }


    protected void onDismiss() {

    }

    protected Dialog createDialog(Context context) {
        return new Dialog(context, R.style.myDialog);
    }


    public MyDialog(@NonNull Context context, @NonNull View contentView) {
        this(context, contentView, true);
    }


    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            Window window = dialog.getWindow();
//            ((Activity) mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向

            dialog.show();



        }
    }

    public void showFull() {
        if (dialog != null && !dialog.isShowing()) {
            Window window = dialog.getWindow();
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);//设置横向
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    public void showFromBottom() {
        if (dialog != null && !dialog.isShowing()) {
            Window window = dialog.getWindow();      // 得到dialog的窗体
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.BOTTOM);


//            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
//            window.getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
//                int uiOptions =
//                        //全屏
//                        View.SYSTEM_UI_FLAG_FULLSCREEN |
//                        //隐藏导航栏
//                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
//                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
//                uiOptions |= 0x00001000;
//                dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
//            });
            WindowManager.LayoutParams atrrs = window.getAttributes();
            atrrs.width = WindowManager.LayoutParams.MATCH_PARENT;
            atrrs.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(atrrs);
            window.setWindowAnimations(R.style.BottomAnimation);


            dialog.show();
        }

    }


    public boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }

    @Override
    public void onClick(View v) {
        if (ViewClickUtil.doubleClick(v)) {
            return;
        }
        onViewClick(v);
    }

    public void onViewClick(@NonNull View v) {

    }
}
